const { SyncHook, SyncBailHook, SyncLoopHook } = require("tapable");

let count = 0;

class Compiler {
  constructor() {
    this.hook = {
      // 1.创建hooks
      syncHook: new SyncHook(["name", "age"]),
      // bail的特点: 如果有返回值, 那么可以阻断后续事件继续执行
      bailHook: new SyncBailHook(["name", "age"]),
      // loop的特点: 如果有返回值, 那么会继续执行, 直到没有返回值为止
      loopHook: new SyncLoopHook(["name", "age"]),
    };

    // 2.用hooks监听事件(自定义plugin)
    this.hook.loopHook.tap("event1", (name, age) => {
      if (count < 5) {
        count++;
        console.log("event1:", name, age);
        return "event1";
      }
    });

    this.hook.loopHook.tap("event2", (name, age) => {
      console.log("event2:", name, age);
    });
  }
}

const compiler = new Compiler();
setTimeout(() => {
  compiler.hook.loopHook.call("zhangsan", 18);
}, 2000);
